package com.atguigu.srb.sms.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.common.exception.Assert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.utils.RandomUtils;
import com.atguigu.srb.mq.pojo.dto.SmsDTO;
import com.atguigu.srb.sms.service.SmsService;
import com.atguigu.srb.sms.utils.SmsProperties;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Resource
    RedisTemplate redisTemplate;
    @Override
    public void send(String mobile) {
         //*创建远程连接客户端对象 配置公共参数*//
        DefaultProfile profile = DefaultProfile.getProfile(
                SmsProperties.REGION_Id,
                SmsProperties.KEY_ID,
                SmsProperties.KEY_SECRET);

        //请求参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysRegionId(SmsProperties.REGION_Id);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("SignName",SmsProperties.SIGN_NAME);
        request.putQueryParameter("PhoneNumbers",mobile);
        request.putQueryParameter("TemplateCode",SmsProperties.TEMPLATE_CODE);
        String sixBitRandom = RandomUtils.getSixBitRandom();
        Map<String,Object> map = new HashMap<>();
        map.put("code",sixBitRandom);
        Gson gson = new Gson();
        String code = gson.toJson(map);
        request.putQueryParameter("TemplateParam",code);
        IAcsClient client = new DefaultAcsClient(profile);

        try {
            CommonResponse commonResponse = client.getCommonResponse(request);
            boolean success = commonResponse.getHttpResponse().isSuccess();
            if (success) {
            redisTemplate.opsForValue().set("srb:sms:code:" + mobile,sixBitRandom);
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void sendMessage(SmsDTO smsDTO) {
        //*创建远程连接客户端对象 配置公共参数*//
        DefaultProfile profile = DefaultProfile.getProfile(
                SmsProperties.REGION_Id,
                SmsProperties.KEY_ID,
                SmsProperties.KEY_SECRET);

        //请求参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysRegionId(SmsProperties.REGION_Id);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("SignName",SmsProperties.SIGN_NAME);
        request.putQueryParameter("PhoneNumbers",smsDTO.getMobile());
        request.putQueryParameter("TemplateCode",SmsProperties.TEMPLATE_CODE);
        String sixBitRandom =smsDTO.getMessage();
        Map<String,Object> map = new HashMap<>();
        map.put("code",sixBitRandom);
        Gson gson = new Gson();
        String code = gson.toJson(map);
        request.putQueryParameter("TemplateParam",code);
        IAcsClient client = new DefaultAcsClient(profile);
        try {
            CommonResponse commonResponse = client.getCommonResponse(request);
            boolean success = commonResponse.getHttpResponse().isSuccess();
            if (success) {
                redisTemplate.opsForValue().set("srb:sms:code:" + smsDTO.getMobile(),sixBitRandom);
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
